<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    :title="title"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item
            label="名称"
            prop="name"
            :rules="[{ required: true, message: '请输入名称', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-input v-model="form.name" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="开放对象"
            prop="kfdx"
            :rules="[{ required: true, message: '请选择开放对象', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-checkbox-group v-model="fwdx" size="small">
              <el-checkbox v-for="item in dict.service_dx" :key="item.id" :border="true" :label="item.value">{{
                item.label
              }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="可用平台"
            prop="kypt"
            :rules="{ required: true, message: '请选择可用平台', trigger: 'change' }"
            style="width: 100%"
          >
            <el-radio-group v-model="form.kypt" style="width:100%" @change="changeObject">
              <el-radio
                v-for="item in ptList"
                :key="item.id"
                :label="item.id"
              >{{ item.name }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="模版大类"
            prop="mbdl"
            :rules="[{ required: true, message: '请选择模版大类', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.mbdl" clearable placeholder="请选择模版大类" @change="changeObject">
              <el-option
                v-for="item in tempParent"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="模版分类"
            prop="mbfl"
            :rules="[{ required: true, message: '请选择数模版分类', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.mbfl" clearable placeholder="请选择数模版分类" @change="changeObject">
              <el-option
                v-for="item in tempFl"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="模版"
            prop="mb"
            :rules="[{ required: true, message: '请选择模版', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.mb" clearable placeholder="请选择模版">
              <el-option
                v-for="item in template"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="数据来源"
            prop="sjlx"
            :rules="[{ required: true, message: '请选择数据来源', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.sjlx" clearable placeholder="请选择数据来源" @change="dataChange">
              <el-option
                v-for="item in dataTypes"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="数据项"
            prop="sjx"
            :rules="[{ required: true, message: '请选择数据项', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.sjx" clearable placeholder="请选择数据项">
              <el-option
                v-for="item in dataList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="允许游客访问"
            prop="isGuest"
            :rules="{ required: true, message: '请选择当前状态', trigger: 'change' }"
            style="width: 100%"
          >
            <el-radio-group v-model="form.isGuest" style="width: 200px">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateObject, saveObject } from '@/api/portal/manager/columnTemp'
import { queryAllKypt, queryAllFlParent, queryAllFlChild, queryAllMb, querySjlx, queryAllSystemDatasource } from '@/api/portal/manager/setting'
import { queryAllSjx } from '@/api/mobile/data/index'
export default {
  name: 'PortalTempColumnEntity',
  dicts: ['service_dx'],
  data() {
    return {
      title: '大类管理',
      loading: false,
      dialogFormVisible: false,
      fwdx: [],
      ptList: [],
      dataTypes: [],
      dataList: [],
      tempParent: [],
      tempFl: [],
      template: [],
      status: [{ id: 0, value: false, label: '不允许' },
        { id: 1, value: true, label: '允许' }],
      form: {
        id: null,
        name: '',
        kfdx: '',
        pzx: '',
        sjlx: '',
        mbdl: '',
        mbfl: '',
        mb: '',
        kypt: '',
        sjx: null,
        ssfl: null,
        isGuest: false
      }
    }
  },
  watch: {
    fwdx: {
      handler(newName, oldName) { // 值发生变化时
        this.form.kfdx = newName.toString()
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: true // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    addEntity(data) {
      this.form.ssfl = data
      this.title = '新增'
      this.dialogFormVisible = true
      this.initPt()
      this.initAllFlParent()
      this.initDataType()
      this.changeObject()
      this.dataChange()
    },
    openEntity(data) {
      console.log(data)
      this.form = data
      if (undefined !== data && undefined !== data.id && data.id !== null) {
        this.title = '修改信息'
        this.initPt()
        this.initAllFlParent()
        this.initDataType()
        this.changeObject()
        this.dataChange()
        this.dialogFormVisible = true
        if (undefined !== data.kfdx && data.kfdx !== '' && data.kfdx !== null) {
          this.fwdx = data.kfdx.split(',')
        }
      }
    },
    save(formName) {
      this.loading = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.id !== null && undefined !== this.form.id) {
            updateObject(this.form).then(res => {
              this.$message.success('更新成功！')
              this.close()
            }).catch(() => {
              this.loading = false
              this.$message.error('更新失败！')
            })
          } else {
            saveObject(this.form).then(res => {
              this.$message.success('保存成功！')
              this.close()
              this.loading = false
            }).catch(() => {
              this.loading = false
              this.$message.error('保存失败！')
            })
          }
        } else {
          this.loading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    close() {
      this.loading = false
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    },
    initPt() {
      queryAllKypt().then(res => {
        const { data } = res
        this.ptList = data
      })
    },
    initAllFlParent() {
      queryAllFlParent().then(res => {
        const { data } = res
        this.tempParent = data
      })
    },
    initAllFl() {
      if (this.form.mbdl !== '' && this.form.mbdl !== null && this.form.kypt !== '' && this.form.kypt !== null) {
        let pt = 2
        if (this.form.kypt === 'app') {
          pt = 3
        } else if (this.form.kypt === 'all') {
          pt = 2
        } else if (this.form.kypt === 'pc') {
          pt = 4
        }
        queryAllFlChild({ type: pt, dl: this.form.mbdl }).then(res => {
          const { data } = res
          this.tempFl = data
        })
      } else {
        this.tempFl = []
        this.form.mbfl = ''
      }
    },
    initMb() {
      if (this.form.kypt !== null && this.form.kypt !== '' &&
      this.form.mbdl !== null && this.form.mbdl !== '' &&
      this.form.mbfl !== null && this.form.mbfl !== '') {
        queryAllMb({ pt: this.form.kypt, dl: this.form.mbdl, fl: this.form.mbfl }).then(res => {
          const { data } = res
          this.template = data
        })
      } else {
        this.template = []
        this.form.mb = ''
      }
    },
    initDataType() {
      querySjlx().then(res => {
        const { data } = res
        this.dataTypes = data
      })
    },
    changeObject() {
      this.initAllFl()
      this.initMb()
    },
    dataChange() {
      if (this.form.sjlx !== null && this.form.sjlx !== '') {
        if (this.form.sjlx === 1) {
          this.initDataSource()
        } else if (this.form.sjlx === 2) {
          this.initSystemData()
        }
      } else {
        this.form.sjx = null
      }
    },
    initDataSource() {
      queryAllSjx().then(res => {
        const { data } = res
        if (undefined !== data && data !== null) {
          this.dataList = data.data
        }
      })
    },
    initSystemData() {
      queryAllSystemDatasource().then(res => {
        const { data } = res
        this.dataList = data
      })
    }
  }
}
</script>

<style scoped>

</style>
